// 定義一個 Person 實例 // 未簡化 type Person struct { Name string Gender string Age int }
// 針對同資料型別的欄位進行簡化 type Person struct { Name, Gender string Age int }
struct 的宣告和初始化
宣告 struct
前面已經有初步示範如何定義一個 struct 結構,而宣告 struct 型別跟一般變數一樣
1
var p Person // p 現在為 Person 型別的變數
struct 預設值為零。對於 struct 來說,零表所有欄位均設定為其對應的零值。以上述定義好的 Person 舉例,欄位 Name 和 Gender 因型別為 string ,故預設值為 "",而 Age 型別為 int ,故預設為 0。
1 2 3 4 5
var s Person fmt.Println("Person s: ", s) // Person s: { 0} s.Age = 22 s.Name = "Jack" fmt.Println("Person s: ", s) // Person s: {Jack 22}
初始化 struct
我們可以在初始化時給值
1 2 3 4
p := Person{"Tom", "Male", 25} // 存取 p 的 name 屬性 fmt.Printf("The person's name is %s , gender is %s , age is %d\n", p.Name, p.Gender, p.Age) // The person's name is Tom , gender is Male , age is 25
需注意的是: 不能僅初始化一部分欄位
1
var p = Person{"Tom"} // Compiler Error: too few values in struct initializer
p := Person{Age: 25, Name: "Tom", Gender: "Male"} // 存取 p 的 name 屬性 fmt.Printf("The person's name is %s , gender is %s , age is %d\n", p.Name, p.Gender, p.Age) // The person's name is Tom , gender is Male , age is 25
比較兩者的輸出結果並無差異。
初始化 struct 時對欄位命名 (有換行符號分隔多個欄位)
為提高可讀性,可以用換行符號分隔多個欄位,需注意該情形下必須以逗號結尾
1 2 3 4 5
p := Person{ Age: 25, Name: "Tom", Gender: "Male", }
type Student struct { Human // 匿名欄位,Student 預設會包含 Human 下的所有欄位 speciality string }
// 初始化一個學生 tom := Student{Human{"Tom", 25, 120}, "Computer Science"}
// ===== 存取相應的欄位 =====
fmt.Println("His name is ", tom.Name) // His name is Tom fmt.Println("His age is ", tom.Age) // His age is 25 fmt.Println("His weight is ", tom.Weight) // His weight is 120 fmt.Println("His speciality is ", tom.speciality) //His speciality is Computer Science
// ===== 修改欄位相應的值 =====
// 修改主修科目 tom.speciality = "Finance" fmt.Println("Tom changed his speciality") fmt.Println("His speciality is ", tom.speciality) // His speciality is Finance // 修改年齡 fmt.Println("Tom become old") tom.Age = 46 fmt.Println("His age is", tom.Age) // His age is 46 // 修改體重 fmt.Println("Tom is not an athlet anymore") tom.Weight += 60 fmt.Println("His weight is", tom.Weight) // His weight is 180
由上述範例可知,Student 組合了 Human struct 和 string 這些基本型別,實現欄位的繼承。
type Student struct { Human // 匿名欄位,struct Skills // 匿名欄位,自訂的型別 string slice int// 內建型別 int 作為匿名欄位 speciality string }
// 初始化學生 Jenny jenny := Student{Human: Human{"Jenny", 35, 50}, speciality: "Biology"} // 存取相應的欄位 fmt.Println("Her name is ", jenny.name) // Her name is Jenny fmt.Println("Her age is ", jenny.age) // Her age is 35 fmt.Println("Her weight is ", jenny.weight) // Her weight is 50 fmt.Println("Her speciality is ", jenny.speciality) // Her speciality is Biology // 修改 skill 技能欄位 jenny.Skills = []string{"anatomy"} fmt.Println("Her skills are ", jenny.Skills) // Her skills are [anatomy] jenny.Skills = append(jenny.Skills, "pyhon", "golang") fmt.Println("Her skills now are ", jenny.Skills) // Her skills now are [anatomy pyhon golang] // 修改匿名內建型別欄位前, int 預設初始值為 0 fmt.Println("Her preferred number is", jenny.int) // Her preferred number is 0 // 修改匿名內建型別欄位後 jenny.int = 3 fmt.Println("Her preferred number is", jenny.int) // Her preferred number is 3